﻿/// <reference path="../General.js" />
/// <reference path="EventsBase.js" />

var ____a;


function AttachedHandler(obj, type, handler, binding, argList)
{
	this._obj = obj;
	this._type = type;
	this._handler = handler;
	this._binding = binding;
	this._argList = argList;
}

AttachedHandler.ResolveHandler = function ResolveHandler(handler)
{
	if (typeof (handler) == "string")
	{
		var fnName = handler;
		if (fnName.indexOf("(") == -1)
		{
			try
			{
				handler = eval(fnName);
			}
			catch (ex)
			{
				handler = eval("(function(e) { " + fnName + "(e); })");
			}
		}
		else
		{
			handler = function() { eval(fnName); }
		}
		return handler;
	}
	else if (typeof (handler) == "function")
	{
		return handler;
	}
	else
		Throw("The handler has to be a function reference or the name of a function.");
}

AttachedHandler.prototype._obj = null;
AttachedHandler.prototype._type = "";
AttachedHandler.prototype._handler = function() { };
AttachedHandler.prototype._binding = new Object();
AttachedHandler.prototype._fnToCall = function() { };
AttachedHandler.prototype._argList = null;

AttachedHandler.prototype.get_AddType = function get_AddType()
{
	return this._type;
}
AttachedHandler.prototype.get_AddObject = function get_AddObject()
{
	return this._obj;
}
AttachedHandler.prototype.Add = function Add()
{
	this._fnToCall = this._BindHandler(this._argList);
	this._DoAdd();
}
AttachedHandler.prototype._BindHandler = function _BindHandler(argument_list)
{
	if (this._binding != null || (Events._needToDefaultBinding) || typeof (this._handler) == "string")
	{
		var o = this._binding, f = AttachedHandler.ResolveHandler(this._handler), args = argument_list;
		if (o == null)
			o = this._obj;

		return function BoundHandler()
		{
			var args2 = Events._GetArguments(arguments);

			if (Events._needToPassEvent && arguments.length == 0 && window.event != null && arguments.caller == null) // if IE event call
				args2.splice(0, 0, window.event); // push the event (other browsers do this themselves

			return f.apply(o, args.concat(args2));
		};
	}
	return this._handler;
}
AttachedHandler.prototype._DoAdd = function _DoAdd()
{
	Events.AddRaw(this.get_AddObject(), this.get_AddType(), this._fnToCall);
}
AttachedHandler.prototype._Remove = function _Remove()
{
	Events.RemoveRaw(this.get_AddObject(), this.get_AddType(), this._fnToCall);
}

